import axios from 'axios';
import { Dialog } from 'cube-ui'
import Vue from 'vue'
import {getQuery } from '@/utils/auth'
Vue.use(Dialog)

let url = ''
if (process.env.NODE_ENV === 'production'){ //production
    url = 'http://pyy89.com:9292'
    // url = 'http://18.136.192.121:9292'
}else if(process.env.NODE_ENV === 'development'){ //development
    url = 'http://18.136.192.121:9292'
}

const service = axios.create({
    baseURL: url,
    timeout: 400000,
    withCredentials: true
});

// Request interceptors

service.interceptors.request.use(
    (config) => {
        let data=getQuery()
            config.headers['X-AUTH-PROID'] = data.pro_id;
        if(config.params){
            config.params.pro_id=data.pro_id;
        }else if(config.data){
            config.data.pro_id= data.pro_id;
        }
        return config
    },
    (error) => {
        Promise.reject(error);
    },
);
// Response interceptors
service.interceptors.response.use(
    response => {
        /**
         * status为非0是抛错 可结合自己业务进行修改
         */
        return response
    },
    error => {
        if(error.response){
            if(error.response.data.status===2001) {
                Dialog.$create({
                    type: 'alert',
                    content:error.response.data.msg,
                    confirmBtn: {
                        text: '我知道了',
                        active: true
                    }
                }).show()
            }
        }else{
            Object.assign(error,{response:{data:{status:-400}}})
        }

        return Promise.reject(error)
    }
);

export default service;
